package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import welsir.entity.dos.Role;
import welsir.entity.dos.User;
import welsir.entity.vo.ResultMessage;
import welsir.entity.vo.UserNameVO;
import welsir.mapper.RoleMapper;
import welsir.mapper.UserMapper;
import welsir.mapper.UserRoleMapper;
import welsir.service.IUserService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/07 23:04
 **/

@RequestMapping("/userRole")
@RestController
@Api(value = "用户角色Controller")
public class UserRoleController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    /**
     * 获取全部用户角色
     * @return
     */
    @ApiOperation(value = "获取所有用户角色")
    @GetMapping("/getAllUserRole")
    public ResultMessage<List<Role>> getAllUserRole(){
        QueryWrapper<Role> Rolewrapper = new QueryWrapper<>();
        Rolewrapper.select();
        return ResultUtil.data(roleMapper.selectList(Rolewrapper));
    }

    /**
     * 获取全部用户名称
     * @return
     */
    @ApiOperation(value = "获取所有用户名称")
    @GetMapping("/getAllUserName")
    public ResultMessage<List<UserNameVO>> getAllUserName(){
        List<UserNameVO> result = new ArrayList<>();

        QueryWrapper<User> wrapper= new QueryWrapper<User>();
        wrapper.select("username");
        UserNameVO userNameVO = null;
        List<User> users = userMapper.selectList(wrapper);
        for (User user1 : users) {
            userNameVO = new UserNameVO();
            BeanUtils.copyProperties(user1,userNameVO);
            result.add(userNameVO);
        }
        return ResultUtil.data(result);
    }

}
